//
//  ZYAddStrongCouseDetailController.m
//  优学荟管理系统
//
//  Created by edz on 2017/11/27.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "ZYAddStrongCouseDetailController.h"
#import "ZYAddStrongCouseDetailOneCell.h"
#include "ZYAddStrongCouseDetailTowCell.h"
#import "ZYAddStrongCouseDetailThreeCell.h"
#import "ZYAddStrongCouseDetailFourCell.h"
#define kCellOne @"cell"
#define kCellTow @"celll"
#define kCellThree @"cellll"
#define kCellFour @"celllll"
#import "LLHomeHeaderView.h"
#import "LLCourseDetailsMoreFooterView.h"
#import "ZYAddStrongCourseDetailThreeModel.h"
#import "ZYAddStrongCouseDetailFourModel.h"
#import "ZYAddStrongCouseDetailTowModel.h"
#import "ZYAddStrongCouseDetailOneModel.h"
#import "LLOrgCommentCell.h"
#import "LLCommentViewController.h"
@interface ZYAddStrongCouseDetailController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSMutableArray *dataOneSource;
@property (nonatomic,strong) NSMutableArray *dataTowSource;
@property (nonatomic,strong) NSMutableArray *dataThreeSource;
@property (nonatomic,strong) NSMutableArray *dataFourSource;

@property (nonatomic,strong) UIView *allView;
@property (nonatomic,strong) UIImageView *photoImageView;
@property (nonatomic,strong) UILabel *couseLabel;
@property (nonatomic,strong) UILabel *rmbLabel;
@property (nonatomic,strong) UILabel *countLabel;
@property (nonatomic,strong) UILabel *sumLabel;


@property (nonatomic,strong) UIImageView *orgaanizeImageView;
@property (nonatomic,strong) UILabel *organizeNameLabel;
@property (nonatomic,strong) UIButton *yellOrganizebutton;
@property (nonatomic,strong) UIButton *commentButton;


@property (nonatomic,strong) NSDictionary *currentDict;
@property (nonatomic,strong) NSDictionary *dict;






@end

@implementation ZYAddStrongCouseDetailController

- (NSMutableArray *)dataOneSource{
    
    if (_dataOneSource == nil) {
        self.dataOneSource = [NSMutableArray array];
    }
    return _dataOneSource;
    
}

- (NSMutableArray *)dataTowSource {
    
    if (_dataTowSource == nil) {
        self.dataTowSource = [NSMutableArray array];
    }
    
    return _dataTowSource;
    
}



- (NSMutableArray *)dataThreeSource{
    
    if (_dataThreeSource == nil) {
        self.dataThreeSource = [NSMutableArray array];
    }
    
    
    return _dataThreeSource;
    
    
}



- (NSMutableArray *)dataFourSource {
    
    if (_dataFourSource == nil) {
        self.dataFourSource = [NSMutableArray array];
        
    }
    return _dataFourSource;
    
}




- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"课程详情";
    
    [self setNetWork];
    [self getOrgInfoByLessonId];
    [self setUpSuperView];
   
}

- (void)setUpSuperView{
    
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
    self.tableView.dataSource =self;
    self.tableView.delegate = self;
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    
    
    [self.tableView registerClass:[ZYAddStrongCouseDetailOneCell class] forCellReuseIdentifier:kCellOne];
    [self.tableView registerClass:[ZYAddStrongCouseDetailTowCell class] forCellReuseIdentifier:kCellTow];
    [self.tableView registerClass:[ZYAddStrongCouseDetailThreeCell class] forCellReuseIdentifier:kCellThree];
    [self.tableView registerNib:[UINib nibWithNibName:@"LLOrgCommentCell" bundle:nil] forCellReuseIdentifier:@"LLOrgCommentCell"];
    
    
    
    self.allView = [[UIView alloc]initWithFrame:CGRectMake(0, 0,kScreenWidth ,330)];
//    self.allView.backgroundColor = [UIColor redColor];
    self.tableView.tableHeaderView = self.allView;
    self.tableView.estimatedRowHeight = 200;
    
    [self setUpTowSuperView];
    
    
    
}

- (void)setUpTowSuperView{
    
    self.photoImageView = [UIImageView new];
    self.photoImageView.backgroundColor = [UIColor grayColor];
    [self.allView addSubview:self.photoImageView];
    
    [self.photoImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.top.offset(0);
        make.width.offset(kScreenWidth);
        make.height.offset(160);
        
        
    }];
    
    
    self.couseLabel = [UILabel new];
    self.couseLabel.text = @"授权书（加强课）";
//    self.couseLabel.backgroundColor = [UIColor purpleColor];
    self.couseLabel.font = [UIFont systemFontOfSize:14];
    [self.allView addSubview:self.couseLabel];
    
    [self.couseLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.equalTo(self.photoImageView.mas_bottom).offset(15);
    }];
    
    
    self.rmbLabel = [UILabel new];
    self.rmbLabel.text = @"¥10000";
//    self.rmbLabel.backgroundColor = [UIColor purpleColor];
    self.rmbLabel.textColor = [UIColor redColor];
    self.rmbLabel.font = [UIFont systemFontOfSize:12];
    [self.allView addSubview:self.rmbLabel];
    [self.rmbLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.equalTo(self.couseLabel.mas_bottom).offset(10);
    }];
    
    
    
    self.sumLabel = [UILabel new];
    self.sumLabel.text = @"游览量：60";
//    self.sumLabel.backgroundColor = [UIColor purpleColor];
    self.sumLabel.font = [UIFont systemFontOfSize:13];
    [self.allView addSubview:self.sumLabel];
    [self.sumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.allView).offset(-10);
        make.centerY.equalTo(self.couseLabel);
    }];
    
    
    
    
    


    self.countLabel = [UILabel new];
    self.countLabel.text = @"已经报名 20";
//    self.countLabel.backgroundColor = [UIColor purpleColor];
    self.countLabel.font = [UIFont systemFontOfSize:13];
    [self.allView addSubview:self.countLabel];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.allView).offset(-10);
        make.centerY.equalTo(self.rmbLabel);
    }];

    
    
//    @property (nonatomic,strong) UIImageView *orgaanizeImageView;
    self.orgaanizeImageView = [UIImageView new];
    self.orgaanizeImageView.backgroundColor = [UIColor grayColor];
    self.orgaanizeImageView.layer.cornerRadius = 5;
    self.orgaanizeImageView.clipsToBounds = YES;
    [self.allView addSubview:self.orgaanizeImageView];
    
    [self.orgaanizeImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.rmbLabel);
        make.top.equalTo(self.rmbLabel.mas_bottom).offset(15);
        make.width.height.offset(50);
        
        
    }];
    
    

    
    self.organizeNameLabel = [UILabel new];
    self.organizeNameLabel.text = @"机构名称";
//    self.organizeNameLabel.backgroundColor = [UIColor purpleColor];
    self.organizeNameLabel.font = [UIFont systemFontOfSize:14];
    [self.allView addSubview:self.organizeNameLabel];
    
    [self.organizeNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.orgaanizeImageView.mas_right).offset(8);
        make.top.equalTo(self.orgaanizeImageView);
    }];
    
//    @property (nonatomic,strong) UILabel *yellOrganizeLabel;
    self.yellOrganizebutton = [UIButton  buttonWithType:(UIButtonTypeCustom)];
    [self.yellOrganizebutton setTitle:@"金牌课程" forState:(UIControlStateNormal)];
    [self.yellOrganizebutton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.yellOrganizebutton setBackgroundImage:[UIImage imageNamed:@"bg_jingpaijiguo"] forState:(UIControlStateNormal)];
//    self.yellOrganizebutton.backgroundColor = [UIColor purpleColor];
    self.yellOrganizebutton.titleLabel.font = [UIFont systemFontOfSize:13];

    [self.allView addSubview:self.yellOrganizebutton];
    
    [self.yellOrganizebutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.organizeNameLabel);
        make.top.equalTo(self.organizeNameLabel.mas_bottom).offset(10);
    }];
    
    
    
//    @property (nonatomic,strong) UIButton *commentButton;
    

    
    self.commentButton = [UIButton  buttonWithType:(UIButtonTypeCustom)];
    [self.commentButton setTitle:@" 0人评论过" forState:(UIControlStateNormal)];
    [self.commentButton setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [self.commentButton setImage:[UIImage imageNamed:@"gl_nearby-agencies_love"] forState:(UIControlStateNormal)];
//    self.commentButton.backgroundColor = [UIColor purpleColor];
    self.commentButton.titleLabel.font = [UIFont systemFontOfSize:13];
    
    [self.allView addSubview:self.commentButton];
    
    [self.commentButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.yellOrganizebutton);
        make.top.equalTo(self.yellOrganizebutton.mas_bottom).offset(10);
    }];
    
    
    
    
    
}



- (void)setNetWork{
    
  NSString  *url  = [NSString stringWithFormat:@"trainLesson.do?action=reinforceLessonDetail&id=%@",self.ID];
   
    
    [NetManager Get:LLBaseURLs(url) Auth:YES Params:nil Success:^(id Obj) {
        
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
           
            self.currentDict = [Obj valueForKey:@"t"];
            
            self.couseLabel.text = [NSString stringWithFormat:@"%@",[self.currentDict valueForKey:@"name"]];
            self.rmbLabel.text = [NSString stringWithFormat:@"¥%@",[self.currentDict valueForKey:@"price"]];
            
            self.countLabel.text = [NSString stringWithFormat:@"已报名%@",[self.currentDict valueForKey:@"currentCount"]];
            
            self.sumLabel.text = [NSString stringWithFormat:@"游览量：%@",[self.currentDict valueForKey:@"accessCount"]];
            
            BOOL isUp = [NSString isBlankString:[self.currentDict valueForKey:@"picture"]];
            
            if (isUp) {
                self.orgaanizeImageView.image = [UIImage imageNamed:@"organization_default"];
            }else{
                [self.orgaanizeImageView
                 sd_setImageWithURL:[NSURL URLWithString:[self.currentDict valueForKey:@"picture"]] placeholderImage:[UIImage imageNamed:@"organization_default"] options:0];
            }
            
            
            
            NSArray *arr  = [ self.currentDict valueForKey:@"comments"];
            
            NSArray *setCourseComments = [self.currentDict valueForKey:@"setCourseComments"];
            
            NSArray *elementCount = [self.currentDict valueForKey:@"elementCount"];
            
          
            for (NSDictionary *dict in arr) {
                ZYAddStrongCouseDetailFourModel *model = [ZYAddStrongCouseDetailFourModel new];
                
                [model setValuesForKeysWithDictionary:dict];
                
                [self.dataFourSource addObject:model];
                
            }
            
            
            for (NSDictionary *dictTow in elementCount) {
                
                ZYAddStrongCouseDetailTowModel *model = [ZYAddStrongCouseDetailTowModel new];
                
                [model setValuesForKeysWithDictionary:dictTow];
                [self.dataTowSource addObject:model];
                
                
            }
            
            
            for (NSDictionary *dictThree in setCourseComments) {
                ZYAddStrongCourseDetailThreeModel *model = [ZYAddStrongCourseDetailThreeModel new];
                
                [model setValuesForKeysWithDictionary:dictThree];
                
                [self.dataThreeSource  addObject:model];
            }
            
            
            
            [self.tableView reloadData];
            
        }
        
        
        
        
    } Fail:^(NSError *Error) {
        
    }];
    
    
    
}





#pragma mark ----
- (void)getOrgInfoByLessonId{
    
    NSString *url = [NSString stringWithFormat:@"trainLesson.do?action=getOrgInfoByLessonId&lessonId=%@&longitude=0&latitude=0",self.ID];
    
    NSLog(@"%@ %@",LLBaseURLs(url),[AccountTool shareAccountTool].currentAccount.authCode);
    
    [NetManager Get:LLBaseURLs(url) Auth:YES Params:nil Success:^(id Obj) {
        
        if ([[Obj valueForKey:@"code"] integerValue] == 1000) {
            
            
            self.dict = [Obj valueForKey:@"t"];
            
            
            
             [self.photoImageView sd_setImageWithURL:[NSURL URLWithString:[self.dict valueForKey:@"picture"]] placeholderImage:[UIImage imageNamed:@"organization_default"] options:0];
            
        }
        
    } Fail:^(NSError *Error) {
        
    }];
    
    

    
    
    
    
}






- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 4;
    
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if (0 == section) {
        
        return 1;
    }else if (1 == section){
        
        
        return self.dataTowSource.count;
    }else if (2 == section){
        
        return self.dataThreeSource.count;
    }else{
        
        if (self.dataFourSource.count >= 5) {
            
            return 5;
        }
        
        
      return self.dataFourSource.count;
    }
    
    
    
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (0 == indexPath.section) {
        
        ZYAddStrongCouseDetailOneCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellOne];
        
        BOOL isUp = [NSString isBlankString:[self.currentDict valueForKey:@"description"]];
        
        if (isUp) {
            cell.nameLabel.text = @"";
        }else{
            
            cell.nameLabel.text = [NSString stringWithFormat:@"%@",[self.currentDict valueForKey:@"description"]];
        }
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
        
    }else if (1 == indexPath.section){
        ZYAddStrongCouseDetailTowCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellTow];
        
        if (self.dataTowSource.count > indexPath.row) {
            
            ZYAddStrongCouseDetailTowModel *model = self.dataTowSource[indexPath.row];
            
            [cell assignByZYAddStrongCouseDetailTowModel:model];
             cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        
        return cell;
        
    }else if (2 == indexPath.section){
        ZYAddStrongCouseDetailThreeCell *cell = [tableView dequeueReusableCellWithIdentifier:kCellThree];
        
        if (self.dataThreeSource.count > indexPath.row) {
            ZYAddStrongCourseDetailThreeModel *model = self.dataThreeSource[indexPath.row];
            
            [cell assignByZYAddStrongCourseDetailThreeModel:model];
            
             cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
       
        
        
        
        return cell;
    }else{
        LLOrgCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LLOrgCommentCell"];
        
        if (self.dataFourSource.count > indexPath.row) {
            
            cell.courseCommentModel = self.dataFourSource[indexPath.row];
        }
        
        
         cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (1 == section) {
          return 60;
    }else{
          return 30;
    }
  
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
  
    if (0 == section) {
        
        LLHomeHeaderView *headerView = [[LLHomeHeaderView alloc]init];
        [headerView setTitleImage:@"institutional_details_icon_class_-introduction_default" andTitle:@"课程介绍"  seeName:nil];

        return headerView;
        
    }else if (1 == section){
        UIView *headerView = [UIView new];
        headerView.backgroundColor = [UIColor whiteColor];
        
        
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [button setImage:[UIImage imageNamed:@"icon_yaosu_default"] forState:(UIControlStateNormal)];
        [headerView addSubview:button];
        
        UILabel *nameLabel = [UILabel new];
        nameLabel.text = @"课程加强要素示意图";
        nameLabel.textColor = ColorRGB(0, 84, 173);
        nameLabel.font = [UIFont systemFontOfSize:15];
        [headerView addSubview:nameLabel];
        
        
        UILabel *nameTwoLabel = [UILabel new];
        nameTwoLabel.text = @"三好评价得优统计表";
        nameTwoLabel.textColor = [UIColor grayColor];
        nameTwoLabel.font = [UIFont systemFontOfSize:15];
        [headerView addSubview:nameTwoLabel];
        
        
        
        UILabel *nameThreeLabel = [UILabel new];
        nameThreeLabel.text = @"得优个数";
        nameThreeLabel.textColor = [UIColor grayColor];
        nameThreeLabel.font = [UIFont systemFontOfSize:15];
        [headerView addSubview:nameThreeLabel];
        [button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(10);
            make.top.offset(10);
        }];
        
        [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(button.mas_right).offset(5);
            make.centerY.equalTo(button);
        }];
        
        [nameTwoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.offset(kScreenWidth/2 - 80);
            make.top.equalTo(button.mas_bottom).offset(5);
            make.width.offset(160);
        }];
        
        [nameThreeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(headerView).offset(-15);
            make.centerY.equalTo(nameTwoLabel);
        }];
        
        
        
        
        
        
        
        
        
        
        
 
        return headerView;
        
        
    }else if (2 == section){
        UIView *vv = [UIView new];
        
        UILabel *mm = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, kScreenWidth,20)];
        mm.text = @"需要加强的要素";
        mm.font = [UIFont systemFontOfSize:13];
        [vv addSubview:mm];
        
        
//        LLHomeHeaderView *headerView = [[LLHomeHeaderView alloc]init];
//
//        [headerView setTitleImage:@"icon_yaosu_default" andTitle:@"需要加强的要素" seeName:nil];
        return vv;
    }else{
        
        LLHomeHeaderView *headerView = [[LLHomeHeaderView alloc]init];
        [headerView setTitleImage:@"institutional_details_icon_commend_default" andTitle:@"课程评论" seeName:nil];
        return headerView;
        
    }
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    
    if (3 == section) {
        
        return 40;
    }else{
      
        return 0.0f;
    }

}


-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    if (3 == section) {
        
        
        // __block typeof(self) weakself = self;
        LLCourseDetailsMoreFooterView *view = [[LLCourseDetailsMoreFooterView alloc]init];
        
        
        
        __block typeof(self) __block_self = self;
        view.moreClickedBlock = ^(){
            
            if(__block_self.dataFourSource.count == 0){
                
                [MBProgressHUD showError:@"没有更多评论"];
                
            }else{
                
                LLCommentViewController *vc = [[LLCommentViewController alloc]init];
                
                
                vc.isUp = YES;
                vc.courseCommentModelArray = __block_self.dataFourSource;
                vc.couseId = [NSString stringWithFormat:@"%@",self.ID];
                [__block_self.navigationController pushViewController:vc animated:YES];
                
            }
            
            
        };
        return view;
    }
    
    else
        return nil;
}








@end
